package com.example.bishe.web.b.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.bishe.entity.User;
import com.example.bishe.service.UserService;
import com.example.bishe.util.RestResult;
import com.example.bishe.web.b.vo.getUserPageVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LDH
 * @since 2022/11/30
 */
@RestController
@Api(value = "商户后台", tags = {"用户管理"})
@RequestMapping("/b/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "用户昵称", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "用户创建时间范围--前部分", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "用户创建时间范围--后部分", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "用户启用状态（‘ON’启用，‘OFF’禁用）", paramType = "query"),
            @ApiImplicitParam(name = "roomNum", value = "用户加入的空间数",paramType = "query"),
    })
    @GetMapping("/page")
    public RestResult<IPage<getUserPageVO>> page(
            @RequestParam(required = false,defaultValue = "1")  int pageNum,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long id,
            @RequestParam(required = false) String nikName,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) Integer count
    ){
        IPage<getUserPageVO> userPageVOIPage = userService.userPage(pageNum,pageSize,id,nikName,startTime,endTime,state,count);
        return RestResult.get_success(userPageVOIPage);
    }

    @ApiOperation("启用用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", paramType = "query")
    })
    @PutMapping("/on")
    public RestResult<String> setOn(@RequestParam Long id){
        userService.setON(id);
        return RestResult.set_success("启用成功!");
    }

    @ApiOperation("禁用用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", paramType = "query")
    })
    @PutMapping("/off")
    public RestResult<String> setOff(@RequestParam Long id){
        userService.setOFF(id);
        return RestResult.set_success("禁用成功!");
    }

    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", paramType = "query")
    })
    @DeleteMapping("/delete")
    public RestResult<String> deleteOne(@RequestBody Long id){
        userService.deleteOne(id);
        return RestResult.set_success("删除成功!");
    }

    @ApiOperation("批量删除用户")
    @DeleteMapping("/delBatch")
    public RestResult<Void> deleteBatch(@RequestBody List<User> users){
        userService.deleteBatch(users);
        return RestResult.set_success("批量删除成功！");
    }

}
